<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="120px">
                <el-form-item label="SPU" prop="spu">
                    <el-input v-model.trim="queryParams.spu" placeholder="请输入SPU" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="标题" prop="title">
                    <el-input v-model.trim="queryParams.title" placeholder="请输入标题" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="评论ID" prop="review_id">
                    <el-input v-model.trim="queryParams.review_id" placeholder="请输入评论ID" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="评论时间" prop="date_created">
                    <el-date-picker
                        v-model="queryParams.date_created"
                        format="yyyy-MM-dd"
                        type="daterange"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        style="width: 240px"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="创建时间" prop="create_time">
                    <el-date-picker
                        v-model="queryParams.create_time"
                        format="yyyy-MM-dd"
                        type="daterange"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        style="width: 240px"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <!-- <el-col :span="1.5">
                    <el-button type="warning" plain @click="downloadExcel">下载模板</el-button>
                </el-col> -->
                <!-- <el-col :span="1.5">
                    <upload-product @update="getList"></upload-product>
                </el-col> -->
                <el-col :span="1.5">
                    <upload-button :action="action" text="上传文件" type="success" class="mb8"></upload-button>
                </el-col>
                <!-- <el-col :span="1.5">
                    <el-upload
                        class="upload-demo"
                        ref="upload"
                        :action="action"
                        :headers="headers"
                        :before-upload="handleBeforeUpload"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :on-success="handleUploadSuccess"
                        :on-error="handleUploadError"
                        :show-file-list="false">
                        <el-button slot="trigger" type="success">上传文件</el-button>
                    </el-upload>
                </el-col> -->

                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                </el-col>

                <el-col :span="1.5">
                    <el-button type="danger" plain @click="DeleteSpus">删除SPU</el-button>
                </el-col>

                <right-toolbar :showSearch.sync="showSearch" :columns="columns" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table v-loading="loading"  @selection-change="handleSelectionChange" :data="tableData" row-key="id" border script>
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column label="SPU" prop="spu" min-width="" v-if="showColumn('spu')"></el-table-column>
                <el-table-column label="图片" width="120" v-if="showColumn('shopify_imgs')">
                    <template slot-scope="scope">
                        <ImageUploadImg v-model="scope.row.shopify_normal_imgs" :readOnly="true"></ImageUploadImg>
                    </template>
                </el-table-column>
                <el-table-column label="评论ID" prop="review_id" min-width="" v-if="showColumn('user_name')"></el-table-column>
                <el-table-column label="用户" prop="user_name" min-width="" v-if="showColumn('user_name')"></el-table-column>
                <el-table-column label="标题" prop="title" min-width="" v-if="showColumn('title')"></el-table-column>
                <el-table-column label="内容" prop="content" min-width="" v-if="showColumn('content')" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column label="评分" prop="rating" min-width="30" v-if="showColumn('rating')"></el-table-column>
                <el-table-column label="评论日期" prop="date_created" min-width="" v-if="showColumn('date_created')"></el-table-column>
                <el-table-column label="国家" prop="country" min-width="40" v-if="showColumn('country')"></el-table-column>
                <el-table-column label="创建时间" prop="create_time" min-width="" v-if="showColumn('create_time')"></el-table-column>
                <el-table-column label="创建人" prop="operater" min-width="" v-if="showColumn('operater')"></el-table-column>
                <el-table-column label="操作" width="60" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="importRetry(scope.row)">重试</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList" />
        </div>
    </div>
</template>
<script>
import { querycommentList, exportcomment ,import_url,ReviewsDeleteSpu} from '@/api/order'
import UploadProduct from './components/UploadProduct.vue'
import { exportFile } from '@/utils/index'
import { parseTime } from '@/utils/quickadmin'
import { STATUS_LIST } from './constant'
export default {
    name: 'comment',
    components: {UploadProduct},
    data() {
        return {
            columns: [
                {label:'SPU', prop: 'spu', visible: true },
                {label:'图片', prop: 'shopify_imgs', visible: true },
                {label:'评论ID', prop: 'review_id', visible: true },
                {label:'用户', prop: 'user_name', visible: true },
                {label:'标题', prop: 'title', visible: true },
                {label:'内容', prop: 'content', visible: true },
                {label:'评分', prop: 'rating', visible: true },
                {label:'评论日期', prop: 'date_created', visible: true },
                {label:'国家', prop: 'country', visible: true },
                {label:'创建时间', prop: 'create_time', visible: true },
                {label:'创建人', prop: 'operater', visible: true },
            ],
            action: process.env.VUE_APP_BASE_API + '/customer/Reviews/importInfo',
            downloadUrl: '',
            // 遮罩层
            loading: true,
            download: false,
            // 选中数组
            ids: [],
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 状态数据字典

            dict: {
                STATUS_LIST,
            },
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                title: undefined,
                create_time: [],
                date_created: [],
            },
            multipleSelection: []
        }
    },
    created() {
        this.getList()
    },
    methods: {
        DeleteSpus() {
            if(this.multipleSelection?.length == 0){
                return this.msgError('请勾选再操作')
            }
            this.$confirm(`是否确认删除SPU?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.loading = true
                    ReviewsDeleteSpu({ids: this.multipleSelection}).then(res => {
                        this.$message.success('操作成功')
                        this.getList()
                    }).catch(err => {
                        this.$message.error(err.message)
                    }).finally(() => {
                        this.loading = false
                    })
                })
                .catch(() => {})
        },
        handleSelectionChange(val) {  
            this.multipleSelection = val.map(item => item.id)
        }, 
        importRetry(row) {
            this.$confirm('是否确认重新导入', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(()=>{
                import_url({review_id: row.review_id}).then(()=>{
                    this.$message.success('操作成功')
                    this.handleQuery()
                })
            })
        },

        handleBeforeUpload() {
            this.loading = true
        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePreview(file) {
            console.log(file);
        },
        // 上传成功回调
        handleUploadSuccess(res) {
            this.loading = false
            if (res.code == 200) {
                this.$message.success(res.msg)
            } else {
                this.$message.error(res.msg)
            }
            this.getList();
        },
        // 上传失败
        handleUploadError() {
            this.$message.error('上传失败')
            this.getList();
        },

        showColumn(prop) {
            const data = this.columns.find(c => c.prop === prop)
            return data?.visible ?? true
        },
        toSupply(url) {
            window.open(url, '_blank')
        },
        handleClick() {
            this.getList()
        },
        downloadExcel() {
            window.open(this.downloadUrl, '_blank')
        },
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.date_created?.length == 2) {
                qyparams.date_created = [parseTime(qyparams.date_created[0], 'y-m-d'), parseTime(qyparams.date_created[1], 'y-m-d')]
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            exportcomment(qyparams)
                .then(res => {
                    console.log(res)
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    console.log(err)
                    this.download = false
                })
        },
        showDetailDialog(row, type) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
            this.detailDialog.type = type
        },
        showProductNewAuditDialog(row) {
            this.auditDialog.visible = true
            this.auditDialog.data = row
        },

        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.date_created?.length == 2) {
                qyparams.date_created = [parseTime(qyparams.date_created[0], 'y-m-d'), parseTime(qyparams.date_created[1], 'y-m-d')]
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            querycommentList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
<style scoped>
.border-cell {
    padding: 0 !important;
}
.border-cell .cell {
    padding: 0 !important;
}
.border-cell-item {
    padding: 10px;
    border-bottom: 1px solid #dfe6ec;
}
.border-cell-item:last-child {
    border-bottom: 0;
}
.el-tabs--border-card {
    box-shadow: none;
    border-bottom: 0;
}

::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.table-wrap {
    padding: 15px;
    border-bottom: 1px solid #dfe4ed;
    border-left: 1px solid #dfe4ed;
    border-right: 1px solid #dfe4ed;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
}
</style>
